package com.base.controller;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.base.comm.CommContent;
import com.base.po.voip.Rechargeratios;
import com.base.po.voip.RechargeratiosExample;
import com.base.po.voip.RechargeratiosExample.Criteria;
import com.base.service.LogsService;
import com.base.service.RechargeratiosService;
import com.base.utils.IP;

@Controller
@RequestMapping("/admin")
public class content_xiugaizaixianchongzhi {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private RechargeratiosService rechargeratiosService;
	

	// 账户信息
	@RequestMapping("/content_xiugaizaixianchongzhi")
	public ModelAndView index(HttpServletRequest request,@RequestParam(value = "id", required = true) String id)
			throws Exception {
		// 返回ModelAndView
		System.out.println(id);
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		Rechargeratios rechargeratios =rechargeratiosService.selectByPrimaryKey(id);
		if (rechargeratios == null || rechargeratios.getId() == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		RechargeratiosExample rechargeratiosExample= new RechargeratiosExample();
		rechargeratiosExample.setOrderByClause("Grade DESC");
		Criteria a = rechargeratiosExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Rechargeratios> rechargeratiosList = rechargeratiosService.selectByExample(rechargeratiosExample);
		
		System.out.println(rechargeratios.toString());
		request.setAttribute("id", id);
	    request.setAttribute("money", rechargeratios.getMoney());
	    request.setAttribute("alimoney", rechargeratios.getAlimoney());
	    request.setAttribute("shopmoney", rechargeratios.getShopmoney());
	    request.setAttribute("memo", rechargeratios.getMemo());
	    request.setAttribute("probationperiod", rechargeratios.getProbationperiod());
	    request.setAttribute("pic", rechargeratios.getPic());
	    request.setAttribute("grade", rechargeratios.getGrade());
	    request.setAttribute("rechargeratiosList", rechargeratiosList);
	 
      
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
		} else {
			modelAndView.setViewName("jsp/content_xiugaizaixianchongzhi");
		}
		return modelAndView;
	}

	String SUCCESS = "success";

	// 更改商户信息
	@RequestMapping(value = "/updateRechargeratios", method = RequestMethod.POST)
	public void updateRechargeratios(HttpServletRequest request, HttpServletResponse response, 
			@RequestParam(value = "id", required = true) String id, String agentid, Integer money, 
			Integer alimoney, Integer grade, String pic, Boolean datastatus, Integer shopmoney, 
			String memo, String probationperiod) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			Rechargeratios record = new Rechargeratios(id, agentid, money, alimoney, grade, 
					pic, datastatus, shopmoney, memo, probationperiod);
			int code = rechargeratiosService.updateByPrimaryKeySelective(record);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			
			if (code == 1) {
				logsService.insert(CommContent.TYPE_UPDATE, "修改了1条在线充值信息", usernamesys, true,
						IP.getIpAddr(request));
				out.print(SUCCESS);
			} else {
				logsService.insert(CommContent.TYPE_UPDATE, "修改在线充值信息失败", usernamesys, true,
						IP.getIpAddr(request));
				out.print("更改失败，请您重试");
			}
		}
	}

}
